package com.ruix.app.ui.app;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.RuiApplication;
import com.ruix.app.cache.CacheFactory;
import com.ruix.app.manager.ClientInfo;
import com.ruix.app.manager.UserManager;
import com.ruix.app.mapper.OssTokenMapper;
import com.ruix.app.mapper.SystemConfigMapper;
import com.ruix.app.mapper.UserMapper;
import com.ruix.app.model.AppUpgradeConfig;
import com.ruix.app.model.OssToken;
import com.ruix.app.model.SystemConfig;
import com.ruix.app.model.User;
import com.ruix.app.model.WXUserInfo;
import com.ruix.app.network.DefaultRXObserver;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.service.MusicService;
import com.ruix.app.service.PlayService;
import com.ruix.app.service.PlayServiceConnection;
import com.ruix.app.ui.base.BaseActivity;
import com.ruix.app.ui.main.MainActivity;
import com.ruix.app.ui.user.BindPhoneActivity;
import com.ruix.app.ui.user.ImportUserInfoActivity;
import com.ruix.app.ui.user.LoginActivity;
import com.ruix.app.util.LogUitl;
import com.ruix.app.util.ThreadManager;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/4/10.
 */

public class SplashActivity extends BaseActivity {


    private boolean isLogin = false;
    private boolean checkFinish;
    private PlayServiceConnection mPlayServiceConnection;

    @Override
    protected int loadViewLayout() {
        return R.layout.activity_splash;
    }


    @Override
    protected boolean isUseBaseTitleBar() {
        return false;
    }

    @Override
    protected boolean isUseTransStatusBar() {
        return false;
    }


    @Override
    protected void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    @Override
    protected void initLocalData() {
        startService();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                bindService();
            }
        },1000);
    }

    private void startService() {
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
//        mPlayServiceConnection = new PlayServiceConnection();
//        bindService(intent, mPlayServiceConnection, Context.BIND_AUTO_CREATE);
    }

    private void bindService() {
        Intent intent = new Intent();
        intent.setClass(this, MusicService.class);
        mPlayServiceConnection = new PlayServiceConnection();
        bindService(intent, mPlayServiceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void initListener() {

    }

    protected int statusBarTintColor() {
        return R.color.white;
    }


    @Override
    protected void onResume() {
        super.onResume();
//        launchApp();
        laodSystemInfo();
    }

    public void launchApp() {
        ThreadManager.getScheduledThreadPool().schedule(new Runnable() {
            @Override
            public void run() {
                loadMain();
            }
        }, 2000, TimeUnit.MILLISECONDS);
    }

    private void guideView() {
        Intent intent = new Intent(SplashActivity.this, GuidanceActivity.class);
        startActivity(intent);
        finish();
    }

    private void loadMain() {
        Intent intent;
        if (ClientInfo.isFirstLauncherApp(RuiApplication.appVersionCode)){
            intent = new Intent(this, GuidanceActivity.class);
        } else  if (isLogin) {
            intent = new Intent(this, MainActivity.class);
        } else {
            intent = new Intent(this, LoginActivity.class);
        }
        startActivity(intent);
        this.finish();
    }

    private void getOssToken(){
        RequestFactory.getOssToken(new OssTokenMapper(), new DefaultRXObserver<OssToken>());
    }


    private void laodSystemInfo() {
        RequestFactory.systemInfo(new SystemConfigMapper(), new RXObserver<SystemConfig>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SystemConfig value) {
                //// TODO: 2017/5/31
                if (value != null) {
                    isLogin = value.isLogin();
                    User user = value.getUser();

                    if (isLogin &&  user !=null && !user.isImportInfo()) { //登录了为完善用户信息
                        isLogin = false;
                    }
                    User currentUser =  UserManager.getCurrentUser();
                    currentUser.setHasLogin(isLogin);
                    launchApp();
                    AppUpgradeConfig appUpgradeConfig = value.getAppUpgradeConfig();
                    if (appUpgradeConfig != null){
                        CacheFactory.cacheAppUpgradeConfig(appUpgradeConfig);////缓存 离线下载
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                launchApp();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPlayServiceConnection != null) {
            unbindService(mPlayServiceConnection);
        }
    }


}
